package com.example.notificationpt;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText mTitle,mMessage;
    private Button mSend,mClear;
    NotificationManager manager;
    String channelId="simple_notification";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle=findViewById(R.id.title);
        mMessage=findViewById(R.id.message);
        mSend=findViewById(R.id.send);
        mClear=findViewById(R.id.clear);
        manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel=new NotificationChannel(channelId,"simple",NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }

    public void sendNotification(View view) {
        Notification.Builder builder=new Notification.Builder(MainActivity.this,channelId)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setTicker("新的通知")
                .setContentTitle(mTitle.getText().toString())
                .setContentText(mMessage.getText().toString())
                .setDefaults(Notification.DEFAULT_ALL);
        manager.notify(0x01,builder.build());
    }

    public void clearNotification(View view) {
        manager.cancelAll(); //删除所有通知
//        manager.cancel(0x01); //根据id删除通知。
    }
}